// Created by 25429 on 2020/6/8.
//

#ifndef SIRIUS_TRADE_ENGINE_OES_EMBED_H
#define SIRIUS_TRADE_ENGINE_OES_EMBED_H

#include "common_function.h"
#include "engine_base.h"


class strategy_engine;
namespace Quant360{
    class OesClientApi;
}
class OesClientMySpi;

class trade_engine : public engine_base {
public:
    trade_engine(string name, strategy_engine *strategy);

    // 委托状态
    static int order_status(int status);

    static string order_status2(int status);

    // 运行
    void go() override;

    // 查询资产列表
    int req_trade_balances()
    {
        m_trade_queue_slow.enqueue(SiriusQueueField(QUEUE_TRADE_BALANCES));
        return ERROR_OK;
    }

    // 查询持仓
    int req_trade_holdings()
    {
        m_trade_queue_slow.enqueue(SiriusQueueField(QUEUE_TRADE_HOLDING));
        return ERROR_OK;
    }

    // 查询委托
    int req_trade_orders() {
        m_trade_queue_slow.enqueue(SiriusQueueField(QUEUE_TRADE_ORDERS));
        return ERROR_OK;
    }

    // 查询成交
    int req_trade_deals() {
        m_trade_queue_slow.enqueue(SiriusQueueField(QUEUE_TRADE_DEALS));
        return ERROR_OK;
    }

    // 下单
    int req_trade_order_insert(const SiriusTradeReqOrderInsertField &req) {
        m_trade_queue_fast.enqueue(SiriusQueueFastField(QUEUE_TRADE_ORDER_INSERT,req));

        return ERROR_OK;
    }

    // 撤单
    int req_trade_order_cancel(const SiriusTradeReqOrderInsertField &req) {
        m_trade_queue_fast.enqueue(SiriusQueueFastField(QUEUE_TRADE_ORDER_CANCEL, req));
        return ERROR_OK;
    }

public:
    std::mutex m_api_mtx;
    Quant360::OesClientApi* m_api;
    OesClientMySpi* m_spi;
    // 策略引擎
    strategy_engine *m_strategy_engine{};
    // 交易队列
    moodycamel::ConcurrentQueue<SiriusQueueFastField> m_trade_queue_fast;// 消息队列
    moodycamel::ConcurrentQueue<SiriusQueueField> m_trade_queue_slow;// 消息队列
};


#endif //SIRIUS_TRADE_ENGINE_OES_EMBED_H
